//
//  HMEmoticonViewCell.swift
//  Weibo24
//
//  Created by apple on 17/4/6.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
//  自定义表情cell
class HMEmoticonViewCell: UICollectionViewCell {
    
    //  MARK: ---  懒加载控件
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel(fontSize: 20, textColor: UIColor.red, text: "")
        return label
    }()
    
    //  删除按钮
    fileprivate lazy var deleteButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(deleteButtonAction), for: .touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        return button
    }()
    
    //  表情按钮的数组
    fileprivate lazy var emoticonButtonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    //  表情数据
    var emoticonArray: [HMEmoticon]? {
        didSet {
            guard let currentEmoticonArray = emoticonArray else {
                return
            }
            //  默认表情全部隐藏
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            
            
            //  遍历表情数组模型给表情按钮赋值
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                //  根据下标获取表情按钮
                let button = emoticonButtonArray[i]
                //  设置表情模型
                button.emoticon = emoticon
//                button.isHidden = false
//                if emoticon.type == "0" {
//                    
//                    //  图片表情
//                    button.setImage(UIImage(named: emoticon.path!), for: .normal)
//                    button.setTitle(nil, for: .normal)
//                } else {
//                    //  emoji表情
//                    let emoji = (emoticon.code! as NSString).emoji()
//                    button.setTitle(emoji, for: .normal)
//                    button.setImage(nil, for: .normal)
//                }
            }
        }
    }
    
    var indexPath: IndexPath? {
        didSet {
            guard let currentIndexPath = indexPath else {
                return
            }
            
            messageLabel.text = "当前显示的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  添加20个表情按钮
        addChildButton()
        contentView.addSubview(deleteButton)
//        contentView.addSubview(messageLabel)
//        messageLabel.snp.makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        }
    }
    
    //  添加20个表情按钮
    fileprivate func addChildButton() {
        //  提示:如果再此获取不到当前视图的宽度,需要在layoutsubView里面去子控件布局
        for _ in 0..<20 {
            let button = HMEmoticonButton()
            //  点击事件
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            //  设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            
            contentView.addSubview(button)
            //  保存表情按钮
            emoticonButtonArray.append(button)
        }
    }
    
    //  MARK: ---点击表情按钮的逻辑处理
    @objc private func emoticonButtonAction(btn: HMEmoticonButton) {
        let emoticon = btn.emoticon!
        //  发送通知给textView设置值
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoitconButtonNoti), object: emoticon)
    }
    
    //  MARK: ---点击删除按钮的事件
    @objc private func deleteButtonAction() {
        //  发送删除表情的通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNoti), object: nil)
    }
    
    //  完成子控件的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //  每项的宽度
        let buttonWidth = width / 7
        //  每项的高度
        let buttonHeight = height / 3
        
        for (i,button) in emoticonButtonArray.enumerated() {
            //  根据下标计算列索引
            let colIndex = i % 7
            //  根据下标计算行索引
            let rowIndex = i / 7
            //  设置x坐标
            button.x = CGFloat(colIndex) * buttonWidth
            //  设置y坐标
            button.y = CGFloat(rowIndex) * buttonHeight
            //  设置大小
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
        }
        //  设置删除按钮的大小
        deleteButton.size = CGSize(width: buttonWidth, height: buttonHeight)
        deleteButton.x = width - buttonWidth
        deleteButton.y = height - buttonHeight
        
    }
    
    
}
